package com.demo.server;

import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TNonblockingServerTransport;

import com.demo.thrift.ThriftMysqlService;

/**
 * 非阻塞I/O服务
 *
 * @author HuangKe
 * @date 2017年3月7日
 * @company Copyright © 2007-2016 www.zgxcw.com All Rights Reserved
 */
public class NoBlockingServer {
	
	private static final int port = 8899;
	
	public static void main(String[] m) {
		 //创建processor
		 TProcessor tprocessor = new ThriftMysqlService.Processor<ThriftMysqlService.Iface>(new ThriftMyqlServiceImpl());
		 try{
			 //创建transport 非阻塞 nonblocking
			 TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(port);
			 //创建protocol 数据传输协议
			 TCompactProtocol.Factory protocol = new TCompactProtocol.Factory();
			 //创建transport 数据传输方式  非阻塞需要用这种方式传输
			 TFramedTransport.Factory transport = new TFramedTransport.Factory();
			 TNonblockingServer.Args args = new TNonblockingServer.Args(serverTransport);
			 args.processor(tprocessor);
			 args.transportFactory(transport);
			 args.protocolFactory(protocol);
			 //创建服务器 类型是非阻塞
			 TServer server = new TNonblockingServer(args);
			 //开启服务
			 System.out.println("开启服务");
			 server.serve();
			 System.out.println("服务已开启");
		 }catch(Exception e){
		 e.printStackTrace();
		 }
	}
}
